//
//  ModifyBrcastNameVc.m
//  CareRing
//
//  Created  on 2025/6/27.
//
    

#import "ModifyBrcastNameVc.h"
#import <Masonry/Masonry.h>
#import "ConfigModel.h"
#import "CARingSDK.h"
#import "CABLeService.h"
#import "FunctionButton.h"

@interface ModifyBrcastNameVc ()<CABleScanProtocal, CABleDataProtocal>

@property(weak, nonatomic)UITextField *tf;
@property(strong, nonatomic)UISwitch *hidemacSwitch;

@end

@implementation ModifyBrcastNameVc

- (void)viewDidLoad {
    [super viewDidLoad];
    WEAK_SELF
    [self customNavStyleNormal:_L2(L_TITL_MDF_BRCST_NAME) BackBlk:^{
        STRONG_SELF
        [strongSelf.navigationController popViewControllerAnimated:YES];
    }];
    
    [self initUI];
    
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    CARingSDK.instance.blescanDelegate = self;
    CARingSDK.instance.bleDataDelegate = self;
}

-(void)initUI
{
    UITextField *tf = [[UITextField alloc]init];
    [self.view addSubview:tf];
    tf.borderStyle = UITextBorderStyleRoundedRect;
    self.tf = tf;
    tf.placeholder = @"Please enter the broadcast name, length (0-16]";
    NSString *name =CARingSDK.instance.currentDevice.advDataLocalName;
    NSArray<NSString *> *seps = [name componentsSeparatedByString:@"_"];
    
    tf.text = seps.firstObject;
    
    self.hidemacSwitch = [[UISwitch alloc]init];
    UILabel *hidemacLbl = [UILabel new];
    hidemacLbl.text = @"Hide macaddress";
    hidemacLbl.textColor = UIColor.darkTextColor;
    self.hidemacSwitch.on = NO;
    
    FunctionButton *modifyBrcstNameBtn = [FunctionButton functionButton:@"Send cmd to modify"];
    WEAK_SELF
    [modifyBrcstNameBtn addTapBlk:^(FunctionButton * _Nonnull btn) {
        STRONG_SELF
        if (CARingSDK.instance.currentDevice.isConnected) {
            BOOL succ = [CARingSDK.instance functionModifyBRcastName:strongSelf.tf.text HideMac:self.hidemacSwitch.isOn];
            if (succ == NO) {
                [self showAlertWarningWithTitle:@"Tips" Msg:_L2(L_brcst_name_invalid) btnOk:_L2(L_OK) OkBLk:nil CancelBtn:nil CancelBlk:nil Compelete:nil];
            }
            
        } else {
            [LTPHud.Instance showText:_L2(L_TIP_BLE_NOT_CONNECT) Lasting:2];
        }
    }];
    
    [self.view addSubview:modifyBrcstNameBtn];
    [self.view addSubview:hidemacLbl];
    [self.view addSubview:self.hidemacSwitch];
    
    [tf mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(30);
        make.leading.equalTo(tf.superview).offset(20);
        make.trailing.equalTo(tf.superview).inset(20);
    }];
    
    [hidemacLbl mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(tf);
        make.top.equalTo(tf.mas_bottom).offset(15);
    }];
    
    [self.hidemacSwitch mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(hidemacLbl.mas_trailing).offset(50);
        make.centerY.equalTo(hidemacLbl);
    }];
    
    [modifyBrcstNameBtn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@44);
        make.top.equalTo(hidemacLbl.mas_bottom).offset(40);
        make.centerX.equalTo(modifyBrcstNameBtn.superview);
        make.width.equalTo(modifyBrcstNameBtn.superview).multipliedBy(3/4.0f);
    }];
    
}


- (void)caBleCmdExcute:(EXCUTED_CMD)cmd Succ:(BOOL)isSucc Reason:(CA_CMD_EXECTE_ERROR_REASON)reason Data:(nonnull NSData *)originData { 
    
    if (cmd == EXCUTED_CMD_MODIFY_BRCAST_NAME) {
        if (isSucc) {
            // success
            WEAK_SELF
            [self showAlertWarningWithTitle:@"Tip" Msg:_L2(L_mdf_brcst_name_succ_reoot) btnOk:_L2(L_OK) OkBLk:^{
                STRONG_SELF
                [CARingSDK.instance functionRebootDevice];
                [strongSelf.navigationController popViewControllerAnimated:YES];
                
            } CancelBtn:nil CancelBlk:nil Compelete:nil];
        }
        
        if (!isSucc) {
            // fail
            NSString *string = [NSString stringWithFormat:_L2(L_mdf_brcst_name_fail), reason];
            [self showAlertWarningWithTitle:@"Tip" Msg:string btnOk:_L2(L_OK) OkBLk:^{
                
                
            } CancelBtn:nil CancelBlk:nil Compelete:nil];
        }
        
    }
    
}

- (void)caBleDeviceBatteryLevel:(NSInteger)batteryLevel IsCharging:(BOOL)isCharging { 
    
}

- (void)caBleDeviceDidReadyForReadAndWrite:(nonnull CABLeService *)service { 
    
}

- (void)caBleDeviceInfo:(nonnull CADeviceInfo *)devInfo { 
    
}

- (void)caBleDeviceRealtimeSteps:(nonnull NSNumber *)steps IsDeviceFix:(BOOL)isDeviceFixed { 
    
}

- (void)caBleDeviceRealtimeTemperature:(nonnull NSNumber *)temperature { 
    
}

- (void)caBleDidConnectPeripheral:(nonnull CABLeService *)service { 
    
}

- (void)caBleDidDisconnectPeripheral:(nonnull CABLeService *)service { 
    
}

- (void)caBleFailConnectPeripheral:(nonnull CBPeripheral *)peripheral Error:(nonnull NSError *)error { 
    
}

- (void)caBleHistoryDataCount:(NSInteger)count { 
    
}

- (void)caBleHistoryDataTimeout { 
    
}

- (void)caBleHistorySr03Data:(CAHistoryDataSR03 * _Nullable)hisData IsComplete:(BOOL)isComplete {
    
}

- (void)caBleIsBinded:(BOOL)isBinded { 
    
}

- (void)caBleMeasureDuration:(NSInteger)seconds { 
    
}

- (void)caBleOEMAuthResult:(BOOL)authSucceddful { 
    
}

- (void)caBleRealtimeHRV:(nonnull NSNumber *)hrv BloodOxygen:(nonnull NSNumber *)bloodOxygen HeartRate:(nonnull NSNumber *)hr Status:(REALTIME_STATUS)status { 
    
}

- (void)caBleSN:(nonnull NSString *)sn SnData:(nonnull NSData *)snData { 
    
}


@end
